package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.context.BaseContext;
import com.ydd.context.SysBaseContext;
import com.ydd.dto.UpdateUserDTO;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddUser;
import com.ydd.entity.YddUserData;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.mapper.YddUserDataMapper;
import com.ydd.mapper.YddUserMapper;
import com.ydd.result.Result;
import com.ydd.service.YddUserService;
import com.ydd.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {

    @Autowired
    private YddUserMapper userMapper;
    @Autowired
    private YddArticleMapper articleMapper;
    @Autowired
    private YddUserDataMapper dataMapper;
    /**
     * 修改用户资料
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateUser(UpdateUserDTO dto) {
        YddUser userData=new YddUser();
        BeanUtils.copyProperties(dto,userData);
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getId, SysBaseContext.getCurrentId());
        userMapper.update(userData,wrapper);
        return Result.success();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public Result getUserInfo() {
        String userId = SysBaseContext.getCurrentId();
        YddUser user = getById(userId);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getUserId,userId);
        Long count = articleMapper.selectCount(wrapper);
        Long artNum=count;
        Long collectNum = dataMapper.selectCount(new LambdaQueryWrapper<YddUserData>().eq(YddUserData::getUserId, userId).eq(YddUserData::getType, 1));
        Long zabNum = dataMapper.selectCount(new LambdaQueryWrapper<YddUserData>().eq(YddUserData::getUserId, userId).eq(YddUserData::getType, 2));

        UserVO vo = UserVO.builder()
                .userInfo(user)
                .zanNum(zabNum)
                .collectNum(collectNum)
                .artileNum(artNum)
                .build();

        return Result.success(vo);
    }
}
